package site.shihuan.helpdesk.security.context;

import site.shihuan.helpdesk.common.exception.CommonException;
import site.shihuan.helpdesk.common.model.JwtUser;
import site.shihuan.helpdesk.security.exception.AccessException;

/**
 * TODO
 *
 * @author 周世焕
 * @Date 2021/1/27 21:54
 */
public class UserContext {

    public static final ThreadLocal<UserContextHolder> map = new ThreadLocal();

    public static void setContext(UserContextHolder userContextHolder){
        map.set(userContextHolder);
    }

    public static UserContextHolder getContext(){
        UserContextHolder userContextHolder = map.get();
        if(userContextHolder != null) {
            return userContextHolder;
        }
        throw new CommonException(403,"用户未登录");
    }

    public static JwtUser getUser() {
        JwtUser user = getContext().getUser();
        if(user != null) {
            return user;
        }
        throw new CommonException(403,"用户未登录");
    }

    public static String getUserId() {
        return getUser().getUid();
    }

    public static String getTenantId() {
        return getUser().getTenantId();
    }

    public static String getClientId(){
        return getContext().getClientId();
    }
}
